package vn.edu.nuce.bean;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import vn.edu.nuce.model.LichDangKy;
import vn.edu.nuce.model.PhongMay;
import vn.edu.nuce.model.ThongKe;
import vn.edu.nuce.model.TietHoc;
import vn.edu.nuce.service.LichDangKyService;
import vn.edu.nuce.service.PhongMayService;
import vn.edu.nuce.service.TietHocService;
import vn.edu.nuce.service.UserService;

@ManagedBean(name = "thongKeBean")
@ViewScoped
public class ThongKeBean {
	@ManagedProperty(value = "#{LichDangKyService}")
	LichDangKyService lichDangKyService;
	@ManagedProperty(value = "#{TietHocService}")
	TietHocService tietHocService;
	@ManagedProperty(value = "#{PhongMayService}")
	PhongMayService phongMayService;
	@ManagedProperty(value = "#{UserService}")
	UserService userService;

	List<TietHoc> tietHocList;
	List<PhongMay> phongMayList;
	List<LichDangKy> lichDangKyList;

	ThongKe ettThongKe = new ThongKe();

	public void thongKe() {
		boolean quanLy = (Boolean) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get(LoginBean.AUTH_QL);
		if (!quanLy) {
			String userId = (String) FacesContext.getCurrentInstance()
					.getExternalContext().getSessionMap()
					.get(LoginBean.AUTH_ID);
			ettThongKe.setTenGiaoVien(getUserService().getById(userId)
					.getTenUser());
		}
		lichDangKyList = getLichDangKyService().thongKe(ettThongKe);
	}

	public void export() {
		try {
			HSSFWorkbook workBook = new HSSFWorkbook();
			Row row = null;
			Cell cell = null;
			Sheet sheet = workBook.createSheet();
			row = sheet.createRow(0);
			cell = row.createCell(0);
			cell.setCellValue("Thống kê thông tin đăng ký phòng máy ");
			row = sheet.createRow(1);
			cell = row.createCell(0);
			cell.setCellValue("STT");
			cell = row.createCell(1);
			cell.setCellValue("Phòng");
			cell = row.createCell(2);
			cell.setCellValue("Tiết");
			cell = row.createCell(3);
			cell.setCellValue("Giáo viên");
			cell = row.createCell(4);
			cell.setCellValue("Tiêu đề");
			cell = row.createCell(5);
			cell.setCellValue("Mô tả");
			cell = row.createCell(6);
			cell.setCellValue("Yêu cầu");
			cell = row.createCell(7);
			cell.setCellValue("Trạng thái");
			cell = row.createCell(8);
			cell.setCellValue("Lý do hủy");

			int size = lichDangKyList.size();
			LichDangKy lichDangKy;
			for (int rowIndex = 0; rowIndex < size; rowIndex++) {
				lichDangKy = lichDangKyList.get(rowIndex);
				row = sheet.createRow(rowIndex + 2);
				cell = row.createCell(0);
				cell.setCellValue(rowIndex + 1);
				cell = row.createCell(1);
				cell.setCellValue(lichDangKy.getTenPhongMay());
				cell = row.createCell(2);
				cell.setCellValue(lichDangKy.getMoTaTietHoc());
				cell = row.createCell(3);
				cell.setCellValue(lichDangKy.getTenGiaoVien());
				cell = row.createCell(4);
				cell.setCellValue(lichDangKy.getTieuDe());
				cell = row.createCell(5);
				cell.setCellValue(lichDangKy.getMoTa());
				cell = row.createCell(6);
				cell.setCellValue(lichDangKy.getYeuCau());
				cell = row.createCell(7);
				cell.setCellValue(LichDangKyBean.convertStatus(lichDangKy
						.getStatus() + ""));
				cell = row.createCell(8);
				cell.setCellValue(lichDangKy.getLyDoHuy());
			}

			String fileName = "thongke.xls";
			FileOutputStream fos = new FileOutputStream(fileName);
			workBook.write(fos);
			fos.close();
			String contentType = "application/vnd.ms-excel";
			FacesContext fc = FacesContext.getCurrentInstance();
			HttpServletResponse response = (HttpServletResponse) fc
					.getExternalContext().getResponse();
			response.reset();
			response.setHeader("Content-disposition", "attachment; filename="
					+ fileName);
			response.setContentType(contentType);
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(fileName));
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			int c;
			while ((c = bis.read()) > -1) {
				baos.write(c);
			}
			bis.close();
			OutputStream out = response.getOutputStream();
			out.write(baos.toByteArray());
			out.flush();
			out.close();
			fc.responseComplete();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public List<LichDangKy> getLichDangKyList() {
		return lichDangKyList;
	}

	public void setLichDangKyList(List<LichDangKy> lichDangKyList) {
		this.lichDangKyList = lichDangKyList;
	}

	public List<TietHoc> getTietHocList() {
		tietHocList = getTietHocService().getTietHocList();
		return tietHocList;
	}

	public void setTietHocList(List<TietHoc> tietHocList) {
		this.tietHocList = tietHocList;
	}

	public List<PhongMay> getPhongMayList() {
		phongMayList = getPhongMayService().getPhongMayList();
		return phongMayList;
	}

	public void setPhongMayList(List<PhongMay> phongMayList) {
		this.phongMayList = phongMayList;
	}

	public LichDangKyService getLichDangKyService() {
		return lichDangKyService;
	}

	public void setLichDangKyService(LichDangKyService lichDangKyService) {
		this.lichDangKyService = lichDangKyService;
	}

	public TietHocService getTietHocService() {
		return tietHocService;
	}

	public void setTietHocService(TietHocService tietHocService) {
		this.tietHocService = tietHocService;
	}

	public PhongMayService getPhongMayService() {
		return phongMayService;
	}

	public void setPhongMayService(PhongMayService phongMayService) {
		this.phongMayService = phongMayService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ThongKe getEttThongKe() {
		return ettThongKe;
	}

	public void setEttThongKe(ThongKe ettThongKe) {
		this.ettThongKe = ettThongKe;
	}

}
